

clear all
set mem 2000m
set maxvar 30000

local costratio 20

local keepyears 2000 2005

set more off

foreach y in 21 21w {

forval x = 2/3 {

clear
use output/chris_predict_one_onset_int`x'_to15_alpha3_beta001_all_both

tsset countryid year

drop conflict
	merge 1:1 countryid year using "complete_main_new.dta", keepusing(bdbest25 bdbest1000)
	
	
						
	if `x' == 2 {
						
		quietly gen conflict = bdbest25
							
	}
						
	if `x' == 3 {
							
							
		quietly gen conflict = bdbest1000
							
	}

	*drop if country == ""

	sort countryid year
	
	gen total_conflict = 0	
	
	tsset countryid year
	generate one_before=conflict==0&F1.conflict==1
	generate two_before=conflict==0&(F1.conflict==1|F2.conflict==1)

	
	
	drop if year < 1975	



drop if conflict == 1
drop if p_one_model`y' == .

keep year country* conflict one_before p_one_model`y'


local keepem year == 2010  
foreach j in `keepyears' {
	local keepem `keepem' | year == `j'
}
	

keep if `keepem'

gsort -p_one_model`y'

gen uff = -p_one_model`y'
xtile ptile = uff, nq(100)
egen rank_on_myvar = rank(uff), by(year)
drop uff 

sort year ptile



drop p_one_model`y'


keep if rank_on_myvar <= 5 | one_before == 1


drop rank_on_myvar

keep country one_before ptile year

foreach k in country one_before ptile year {
	rename `k' `k'_`y'
}

gen id = _n
sort id
save output/model`y'_conflict`x', replace



}

}

clear
use output/model21_conflict2

merge id using output/model21w_conflict2
drop id _merge

save output/temp, replace

clear
use output/model21_conflict3

merge id using output/model21w_conflict3
drop id _merge


append using output/temp


texsave * using "output/tableH1.tex",   ///
 title("Descriptive Statistics of Sample") ///
 varlabels marker(tab: case_study2010) location(h) frag align(lccclccc)   ///
 replace

